# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: jshin


class LastActionTimestamp(object):
  def __init__(self):
    self._last_sell_timestamp = 0
    self._last_buy_timestamp = 0
    self._last_timestamp = 0
    self._num_sell_actions = 0
    self._num_buy_actions = 0
    self._num_actions = 0

  @property
  def num_actions(self):
    return self._num_actions

  @property
  def num_sell_actions(self):
    return self._num_sell_actions

  @property
  def num_buy_actions(self):
    return self._num_buy_actions

  def set_last_sell_action(self, timestamp):
    self._last_sell_timestamp = timestamp
    self._num_sell_actions += 1
    if self._last_timestamp < timestamp:
      self._num_actions += 1
      self._last_timestamp = timestamp
    elif self._last_timestamp == timestamp:
      pass
    else:
      raise ValueError(("cannot reverse", self._last_timestamp, timestamp))

  def set_last_buy_action(self, timestamp):
    self._last_buy_timestamp = timestamp
    self._num_buy_actions += 1
    if self._last_timestamp < timestamp:
      self._num_actions += 1
      self._last_timestamp = timestamp
    elif self._last_timestamp == timestamp:
      pass
    else:
      raise ValueError(("cannot reverse", self._last_timestamp, timestamp))

  def within_sell_action_period(self, action_period, timestamp):
    in_period = False
    if (timestamp - self._last_sell_timestamp <= action_period):
      in_period = True
    return in_period

  def within_buy_action_period(self, action_period, timestamp):
    in_period = False
    if (timestamp - self._last_buy_timestamp <= action_period):
      in_period = True
    return in_period
